package com.yiren.dbaa.module.practice_set.site.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.site.contract.ReleaseContract;
import com.yiren.dbaa.module.practice_set.site.presenter.ReleasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/29 10:44
 * @description: 规则发布
 */
public class ReleaseActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_site_name)
    TextView tvSiteName;
    @BindView(R.id.tv_base_version)
    TextView tvBaseVersion;
    @BindView(R.id.tv_update_detail)
    TextView tvUpdateDetail;
    @BindView(R.id.et_update_detail)
    EditText etUpdateDetail;
    @BindView(R.id.ll_updatedetail_tv)
    LinearLayout llUpdatedetailTv;
    @BindView(R.id.ll_updatedetail_et)
    LinearLayout llUpdatedetailEt;
    @BindView(R.id.sp_update_way)
    Spinner spUpdateWay;
    @BindView(R.id.ll_update_way)
    LinearLayout llUpdateWay;
    @BindView(R.id.sp_update_user)
    Spinner spUpdateUser;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;
    @BindView(R.id.ll_update_user)
    LinearLayout llUpdateUser;

    private SiteEntity site;
    private ReleaseContract.Presenter releasePresenter;
    private UserParamConfigBean userParamConfigBean;
    private int updateMode = 1;//更新方式 1:提示更新 2:强制更新

    private List<String> updateModes = new ArrayList<>();//更新方式
    private List<String> updateUsers = new ArrayList<>();//更新用户
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_siterule);
        ButterKnife.bind(this);
        tvTitle.setText("考场规则发布");
        site = (SiteEntity) getIntent().getSerializableExtra("site");
        init();
    }

    private void init() {
        releasePresenter = new ReleasePresenter(this);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(this);
        if (site != null) {
            tvSiteName.setText(site.getSiteName());
            tvBaseVersion.setText("V" + site.getBaseVer() + "");
            if (site.getUseRange() == 1) {
                tvUsingRangeBt.setVisibility(View.VISIBLE);
            } else {
                tvUsingRangeBt.setVisibility(View.GONE);
            }
            if (site.getRuleVer() == 0) {
                tvUpdateDetail.setText("首次发布");
                llUpdateUser.setVisibility(View.GONE);
                llUpdateWay.setVisibility(View.GONE);
                llUpdatedetailTv.setVisibility(View.VISIBLE);
                llUpdatedetailEt.setVisibility(View.GONE);
            } else {
                llUpdateUser.setVisibility(View.VISIBLE);
                llUpdateWay.setVisibility(View.VISIBLE);
                llUpdatedetailTv.setVisibility(View.GONE);
                llUpdatedetailEt.setVisibility(View.VISIBLE);
            }
        }
        //数据源
        updateModes.add("提示更新");
        updateModes.add("强制更新");
        adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateModes);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateWay.setAdapter(adapter);
        spUpdateWay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                updateMode = position + 1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        updateUsers.add("个别用户");
        updateUsers.add("全部用户");
        adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, updateUsers);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spUpdateUser.setAdapter(adapter);
        if (site.getUseRange() == 2) {
            spUpdateUser.setSelection(1);
        } else {
            spUpdateUser.setSelection(0);
        }
        spUpdateUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                site.setUseRange(position + 1);
                if (position == 0) {
                    tvUsingRangeBt.setVisibility(View.VISIBLE);
                } else {
                    tvUsingRangeBt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_using_range_bt, R.id.rl_release_site})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_using_range_bt:

                break;
            case R.id.rl_release_site:
                releasePresenter.releaseSiteRule(site, userParamConfigBean.getUserInfo().getId().toString(),
                        etUpdateDetail.getText().toString(), updateMode);
                break;
        }
    }
}
